<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\ProjectLockPerOrgPeople;
use app\api\model\ProjectPerOrg as ProjectPerOrgModel;
use app\api\model\ProjectPerOrgPeople as ProjectPerOrgPeopleModel;
use app\api\model\ProjectLockPerOrg as ProjectLockPerOrgModel;
use app\Request;

class PerOrg extends Base
{

    /**
     * 考核组织列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function orgList(Request $request)
    {
        try {
            $pid = $request->param('pid');
            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $ProjectPerOrgModel = new ProjectPerOrgModel;
            $list = $ProjectPerOrgModel->alias('p')
                ->field('*')
                ->where('pid','=',$pid)
                ->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ]);
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * depart_spot为1的组织列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function  orgListTj(Request $request){
        try {

            $pid = $request->param('pid');
            $uid = $request->param('uid');
            $page = $request->param('page');
            $month = $request->param('month');
            $month = strtotime(date('Y-m',strtotime($month)));
            $pagesize = $request->param('pagesize');

            //查询该用户属于哪一个部门
            $ProjectLockPerOrgModel = new ProjectLockPerOrgModel;
            $ProjectLockPerOrgPeopleModel = new ProjectLockPerOrgPeople();
            $ProjectLockPerOrgPeopleModelCount = $ProjectLockPerOrgPeopleModel->where('month','like',$month)->count();
            global $findDepart_g;
            if(!$ProjectLockPerOrgPeopleModelCount){
                $ProjectLockPerOrgModel = new ProjectPerOrgModel();
                $ProjectLockPerOrgPeopleModel = new ProjectPerOrgPeopleModel();
                $userinfo_t =  $ProjectLockPerOrgPeopleModel->where('id',$uid)->find();
                $userinfo =  $ProjectLockPerOrgPeopleModel->where('uid',$userinfo_t['uid'])->where('is_outer','=',0)->find();
                $this->findDepart($userinfo['orgid'],$ProjectLockPerOrgModel,"");
                $uinfo = $findDepart_g;
            }else{
                $userinfo_t =  $ProjectLockPerOrgPeopleModel->where('id',$uid)->where('month','like',$month)->find();
                $sql = $ProjectLockPerOrgPeopleModel->getLastSql();
                $userinfo =  $ProjectLockPerOrgPeopleModel->where('uid',$userinfo_t['uid'])->where('month','like',$month)->where('is_outer','=',0)->find();
                $sql = $ProjectLockPerOrgPeopleModel->getLastSql();
                $this->findDepart($userinfo['orgid'],$ProjectLockPerOrgModel,$month);
                $uinfo = $findDepart_g;
            }


            //$pid = $uinfo['id'];


            $ProjectLockPerOrgModel = new ProjectLockPerOrgModel;
            $count_num = $ProjectLockPerOrgModel->where('month','like',$month)->count();
            $sql = $ProjectLockPerOrgModel->getLastSql();

            if(!$count_num){
                $ProjectLockPerOrgModel =  new ProjectPerOrgModel(); //如果没有进行锁定操作，展示实施排名
                $list = $ProjectLockPerOrgModel->alias('p')
                    ->field('*')
                    //->where('depart_spot','=',1)
                    //->where('month','like',$month)
                    ->where('pid','=',$pid)
                    ->paginate([
                        'list_rows'=> $pagesize,
                        'var_page' => 'page',
                        'page'=>$page
                    ]);
            }else{
                $list = $ProjectLockPerOrgModel->alias('p')
                    ->field('*')
                    //->where('depart_spot','=',1)
                    ->where('pid','=',$pid)
                    ->where('month','like',$month)
                    ->paginate([
                        'list_rows'=> $pagesize,
                        'var_page' => 'page',
                        'page'=>$page
                    ]);
            }

            if($list){
                $list = $list->toArray();
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 根据当前orgid获取该用户的主管部门
     * @param $currOrgid
     * @return \think\response\Json
     */
    public function  findDepart($currOrgid,$model,$month){
        try {
            $this->findDepartIn($currOrgid,$model,$month);
            return true;
        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    private function  findDepartIn($pid,$model,$month){
        global $findDepart_g;
        if($month){
            $orginfo = $model->where('id','=',$pid)->where('month','like',$month)->find();
            $sql = $model->getlastsql();
            if($orginfo['depart_spot'] || $pid == 3){
                $findDepart_g = $orginfo;
                return true;
            }else{
                $this->findDepartIn($orginfo['pid'],$model,$month);
            }
        }else {
            $orginfo = $model->where('id','=',$pid)->find();
            $sql = $model->getlastsql();
            if($orginfo['depart_spot'] || $pid == 3){
                $findDepart_g = $orginfo;
                return true;
            }else{
                $this->findDepartIn($orginfo['pid'],$model,$month);
            }
        }
    }
    /**
     * 添加考核组织
     * @param Request $request
     * @return \think\response\Json
     */
    public function addOrg(Request $request){
        try {
            $data['pid'] = $request->param('pid');
            $data['orgname'] = $request->param('orgname');
            $data['create_time'] = time();

            $ProjectPerOrgModel = new ProjectPerOrgModel();
            $res = $ProjectPerOrgModel->insertGetId($data);
            if($res){
                $result = ["status"=>true,"msg"=>"添加成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"添加失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 编辑考核组织
     * @param Request $request
     * @return \think\response\Json
     */
    public function saveOrg(Request $request){
        try {
            $id = $request->param('id');
            if($request->param('pid'))  $data['pid'] = $request->param('pid');
            if($request->param('orgname'))  $data['orgname'] = $request->param('orgname');
            $data['update_time'] = time();

            $ProjectPerOrgModel = new ProjectPerOrgModel();
            $res = $ProjectPerOrgModel->where('id','=',$id)->save($data);

            if($res !== false){
                $result = ["status"=>true,"msg"=>"编辑成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"编辑失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 删除考核组织
     * @param Request $request
     * @return \think\response\Json
     */
    public function delOrg(Request $request){
        try {

            $id = $request->param('id');
            $ProjectPerOrgModel = new ProjectPerOrgModel();
            $res = $ProjectPerOrgModel->where('id','=',$id)->delete();
            if($res){
                $result = ["status"=>true,"msg"=>"删除成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"删除失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
}
